//
//  HEProtocalView.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/10/12.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEProtocalViewController.h"

@interface HEProtocalViewController()

@property (strong, nonatomic) UIView *protocal;

@end

@implementation HEProtocalViewController

-(void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
}

- (void)initUI {
    
    CGFloat headHeight = 48;

    // 初始化隐私政策
    _protocal =[[UIView alloc] init];
    UIColor *color = [UIColor blackColor];
    _protocal.backgroundColor = [color colorWithAlphaComponent:0.3];
    [self.view addSubview:_protocal];
    _protocal.sd_layout
    .topEqualToView(self.view)
    .leftEqualToView(self.view)
    .rightEqualToView(self.view)
    .bottomEqualToView(self.view);
    
    // 内容父视图
    UIView *contentView =[[UIView alloc] init];
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.layer.masksToBounds = YES;
    contentView.layer.cornerRadius = 12;
    [_protocal addSubview:contentView];
    contentView.sd_layout
    .widthIs(336)
    .heightIs(380)
    .centerXEqualToView(_protocal)
    .centerYEqualToView(_protocal);
    
    // 头部视图
    UILabel *headView =[[UILabel alloc] init];
    headView.text = @"温馨提示";
    headView.textAlignment = NSTextAlignmentCenter;
    headView.font = UIFontBoldMake(20);
    [contentView addSubview:headView];
    headView.sd_layout
    .heightIs(headHeight)
    .topEqualToView(contentView)
    .leftEqualToView(contentView)
    .rightEqualToView(contentView);
    
    // 加上分隔线
    CALayer *headerViewSeparatorLayer = [CALayer layer];
    [headerViewSeparatorLayer qmui_removeDefaultAnimations];
    headerViewSeparatorLayer.backgroundColor = UIColorMake(222, 224, 226).CGColor;
    [contentView.layer addSublayer: headerViewSeparatorLayer];
    headerViewSeparatorLayer.frame = CGRectFlatMake(0, headHeight, CGRectGetWidth(self.view.bounds), PixelOne);
    
    // 中间视图
    YYLabel *middle =[[YYLabel alloc] init];
    middle.textVerticalAlignment = YYTextVerticalAlignmentTop;
    middle.textAlignment = NSTextAlignmentLeft;
    [contentView addSubview:middle];
    
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:18], NSForegroundColorAttributeName: [UIColor blackColor]};

    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"请您务必审慎阅读、充分理解服务协议和隐私政策各条款，包括但不限于：为了向您提供各种服务，我们需要收集您的设备信息及一些个人信息。您可以在\"我的\"中查看、变更个人信息。您可以阅读《服务协议》和《隐私政策》了解详细信息。\n如您同意，请点击同意开始接受我们的服务。" attributes:attributes];
    

    //设置高亮色和点击事件
    [text yy_setTextHighlightRange:[[text string] rangeOfString:@"《服务协议》"] color:CNavBgColor backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [self toSeeProtcal:@"https://www.honglutec.com/agreement/service.html"];
    }];

    //设置高亮色和点击事件
    [text yy_setTextHighlightRange:[[text string] rangeOfString:@"《隐私政策》"] color:CNavBgColor backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [self toSeeProtcal:@"https://www.honglutec.com/agreement/yszc.html"];
    }];
    middle.numberOfLines = 0;
    middle.attributedText = text;
    
    
    middle.sd_layout
    .topSpaceToView(contentView, headHeight + PixelOne + 20)
    .leftSpaceToView(contentView, 15)
    .rightSpaceToView(contentView, 15)
    .bottomSpaceToView(contentView, 0);
    

    // 底部按钮
    UIView *btnWarp = [UIView new];
    [contentView addSubview:btnWarp];
    
    btnWarp.sd_layout
    .widthIs(280)
    .heightIs(40)
    .centerXEqualToView(contentView)
    .bottomSpaceToView(contentView, 30);

    QMUIGhostButton *refuseBtn = [[QMUIGhostButton alloc] initWithGhostColor:CNavBgColor];
    refuseBtn.titleLabel.font = UIFontMake(18);
    [refuseBtn setTitle:@"拒绝" forState:UIControlStateNormal];
    [btnWarp addSubview:refuseBtn];

    QMUIFillButton *agreeBtn = [[QMUIFillButton alloc] initWithFillColor:CNavBgColor titleTextColor:CNavBgFontColor];
    agreeBtn.titleLabel.font = UIFontMake(18);
    [agreeBtn setTitle:@"同意" forState:UIControlStateNormal];
    [btnWarp addSubview:agreeBtn];
    
    [refuseBtn addTarget:self action:@selector(refuse:) forControlEvents:UIControlEventTouchUpInside];
    [agreeBtn addTarget:self action:@selector(agreen:) forControlEvents:UIControlEventTouchUpInside];

    
    refuseBtn.sd_layout
    .leftEqualToView(btnWarp)
    .widthIs(120)
    .heightIs(40)
    .centerYEqualToView(btnWarp);
    
    agreeBtn.sd_layout
    .rightEqualToView(btnWarp)
    .widthIs(120)
    .heightIs(40)
    .centerYEqualToView(btnWarp);
}


- (void)toSeeProtcal:(NSString *)protcalName {
//    NSString * fileUrl = [[NSBundle mainBundle]pathForResource:protcalName ofType:@"html"];
//    NSURL * file = [NSURL fileURLWithPath:fileUrl];
    
    NSURL *file = [NSURL URLWithString:protcalName];
  
    RootWebViewController *webView = [[RootWebViewController alloc] initWithUrl:file.absoluteString];
    webView.isShowCloseBtn = YES;
    webView.showPageTitle = YES;
    RootNavigationController *nav = [[RootNavigationController alloc] initWithRootViewController:webView];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
//    nav.navigationController.navigationBar.barTintColor = [UIColor whiteColor];

//    [webView.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
//    [webView.navigationController.navigationBar setTintColor:[UIColor blackColor]];
//    [webView.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName :[UIColor blackColor], NSFontAttributeName : [UIFont systemFontOfSize:18]}];
    
     [self presentViewController:nav animated:YES completion:nil];
}


- (void)refuse:(id)sender {
    [WOAlert alertShowTitle:@"" message:@"您需要同意《服务协议》和《隐私政策》，才能继续使用我们的服务哦。" cancelButtonTitle:@"暂不" otherButtonTitles:@"去同意" block:^(NSInteger buttonIndex) {
        if (buttonIndex == 0) {
            exit(0);
        }
    }];
}

- (void)agreen:(id)sender {
    
    [kUserDefaults setBool:YES forKey:KHasAgreenProtocal];
    
    [self hide];
}

- (void)hide {
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.3  animations:^{
        weakSelf.protocal.alpha = 0;
    } completion:^(BOOL finished) {
        [weakSelf.view removeFromSuperview];
        [weakSelf dismissViewControllerAnimated:NO completion:nil];
    }];
}

@end
